package com.alibaba.algo.controller.app;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.app.AppInfoServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.CreateAppRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.SelectAppListRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.UpdateAppRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.UpdateAppStateRequestVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 类AppInfoController.java的实现描述：应用 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/appInfo")
public class AppInfoController {

    @Autowired
    private AppInfoServiceImpl appInfoService;

    @PostMapping("/insert")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.APP_INFO,operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody CreateAppRequestVO vo) {
        if (StringUtils.isEmpty(vo.getAppCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getAppName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_APP_NAME_ERROR.getMsg());
        }
        if (ObjectUtils.isEmpty(vo.getAppState())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_STATE_ERROR.getCode(), GlobalResultStatus.PARAM_APP_STATE_ERROR.getMsg());
        }
        return appInfoService.insert(vo);
    }

    @PostMapping("/update")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.APP_INFO,operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody UpdateAppRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_ID_ERROR.getCode(), GlobalResultStatus.PARAM_APP_ID_ERROR.getMsg());
        }
        return appInfoService.update(vo);
    }

    @PostMapping("/delete")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.APP_INFO,operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody List<Long> vo) {
        if (CollectionUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_ID_ERROR.getCode(), GlobalResultStatus.PARAM_APP_ID_ERROR.getMsg());
        }
        return appInfoService.delete(vo);
    }

    @PostMapping("state/open")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.APP_INFO,operateType = OperateTypeEnum.UPDATE)
    public BaseResult open(@RequestBody UpdateAppStateRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getAppId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_ID_ERROR.getCode(), GlobalResultStatus.PARAM_APP_ID_ERROR.getMsg());
        }
        return appInfoService.open(vo);
    }

    @PostMapping("state/close")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.APP_INFO,operateType = OperateTypeEnum.UPDATE)
    public BaseResult close(@RequestBody UpdateAppStateRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getAppId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_APP_ID_ERROR.getCode(), GlobalResultStatus.PARAM_APP_ID_ERROR.getMsg());
        }
        return appInfoService.close(vo);
    }

    @PostMapping("/selectPage")
    public BaseResult selectList(@RequestBody SelectAppListRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getPageIndex())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getMsg());
        }
        if (ObjectUtils.isEmpty(vo.getPageSize())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getMsg());
        }
        return appInfoService.selectPage(vo);
    }

}
